#include "drc_core.h"
#include <iostream>
#include <iomanip>

#include <windows.h>

void print_violation(const drc::DRCViolation& violation) {
    std::cout << "[" << std::setw(7) << violation.severity << "] "
              << violation.rule_name << ": " << violation.message;
    
    if (violation.component.name != "") {
        std::cout << " (Component: " << violation.component.name << ")";
    }
    if (violation.net.name != "") {
        std::cout << " (Net: " << violation.net.name << ")";
    }
    std::cout << "\n";
}

int main() {
    try {
        SetConsoleOutputCP(CP_UTF8);

        // 创建一个示例原理图
        schematic::Schematic schematic;
        schematic.name = "Test Circuit";

        // 创建电源网络
        schematic::Net vdd_net;
        vdd_net.name = "VDD";
        vdd_net.net_type = "signal";  // 故意设置错误的类型来触发规则检查
        
        schematic::Net gnd_net;
        gnd_net.name = "GND";
        gnd_net.net_type = "power";

        // 创建一些组件来测试命名规则
        schematic::Component resistor;
        resistor.name = "R1";  // 正确的命名
        resistor.type = "resistor";

        schematic::Component capacitor;
        capacitor.name = "C2";  // 正确的命名
        capacitor.type = "capacitor";

        schematic::Component bad_ic;
        bad_ic.name = "IC1";  // 错误的命名（应该是U1）
        bad_ic.type = "ic";

        schematic::Component unnamed_component;
        unnamed_component.type = "resistor";  // 没有名称

        // 创建引脚并连接到网络
        schematic::Pin vdd_pin1;
        vdd_pin1.name = "VDD";
        vdd_pin1.type = "power";
        vdd_pin1.net_name = "VDD";
        
        schematic::Pin vdd_pin2;
        vdd_pin2.name = "VDD";
        vdd_pin2.type = "power";
        vdd_pin2.net_name = "VDD";

        // 将引脚添加到组件
        resistor.pins.push_back(vdd_pin1);
        capacitor.pins.push_back(vdd_pin2);

        // 将引脚连接到网络
        vdd_net.connected_pins.push_back(vdd_pin1);
        vdd_net.connected_pins.push_back(vdd_pin2);

        // 将组件和网络添加到原理图
        schematic.components.push_back(resistor);
        schematic.components.push_back(capacitor);
        schematic.components.push_back(bad_ic);
        schematic.components.push_back(unnamed_component);
        schematic.nets.push_back(vdd_net);
        schematic.nets.push_back(gnd_net);

        // 创建DRC管理器
        drc::DRCManager drc_manager;

        // 加载规则
        std::cout << "Loading DRC rules...\n";
        drc_manager.load_rule("rules/power_net_check.py");
        drc_manager.load_rule("rules/component_naming_check.py");

        // 运行所有规则检查
        std::cout << "\nRunning DRC checks...\n";
        auto violations = drc_manager.run_all_checks(schematic);

        // 输出违规信息
        std::cout << "\nFound " << violations.size() << " violations:\n";
        std::cout << "----------------------------------------\n";
        for (const auto& violation : violations) {
            print_violation(violation);
        }

    } catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }

   system("pause");
    return 0;
} 